# -*- coding: utf-8 -*-
'''
@文件    :face.py
@说明    :人脸核身
@时间    :2024/12/12 16:43:18
@作者    :浪晋
@版本    :1.0
'''

from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.faceid.v20180301 import faceid_client, models as faceid_models

import json

"""
腾讯云SDK: tencentcloud-sdk-python
"""


class TencentFaceAuth:
    def __init__(self,SecretId, SecretKey):
        self.SecretId = SecretId
        self.SecretKey = SecretKey
                
    def get_client(self):
        cred = credential.Credential(self.SecretId, self.SecretKey)
        httpProfile = HttpProfile()
        httpProfile.endpoint = "faceid.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        region = ""
        client = faceid_client.FaceidClient(cred, region, clientProfile)
        return client
    
    def IdCardVerification(self, IdCard, Name):
        """身份证二要素核身"""
        client = self.get_client()
        req = faceid_models.IdCardVerificationRequest()
        params = {
            "IdCard": IdCard,
            "Name": Name
        }
        req.from_json_string(json.dumps(params))
        resp = client.IdCardVerification(req)
        return json.loads(resp.to_json_string())

    def IdCardOCRVerification(self, url):
        """
        身份证识别及信息核验  
        {"Result": "0", "Description": "姓名和身份证号一致", "Name": "杨旭日", "IdCard": "430626200012102411", "Sex": "男", "Nation": "汉", "Birth": "2000/12/10", "Address": "湖南省平江县加义镇东南村88号", "RequestId": "d3149af1-85a6-43cc-b343-59415160b58f"}
        """
        client = self.get_client()
        req = faceid_models.IdCardOCRVerificationRequest()
        params = {
            "ImageUrl": url
        }
        req.from_json_string(json.dumps(params))
        resp = client.IdCardOCRVerification(req)
        return json.loads(resp.to_json_string())